package com.lz.football.module.leagueMatch.view
{
	import com.common.utils.DisplayObjectUtil;
	import com.common.utils.Reflection;
	import com.liangzhong.events.ModuleEvent;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.type.LeagueMatchType;
	import com.liangzhong.model.type.MatchType;
	import com.lz.football.module.leagueMatch.ILeagueMatchView;
	
	import common.BaseView.DisplayMouseEventManager;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import lz.controls.core.TitleWindow;

	/**
	 *联赛 
	 * @author lake
	 * 
	 */	
	public class LeagueMatchView extends TitleWindow implements ILeagueMatchView
	{
		/**
		 *国家比赛mc 
		 */		
		private var countryMatchMc :MovieClip;
		/**
		 *标题原始x 
		 */		
		private var countryMatchMcOrX :Number = 0;
		/**
		 *面板容器 
		 */		
		private var panelContainer :Sprite;
		/**
		 *选择的阵容索引 
		 */		
		private var lineUpIndex	:int = 1;
		/**
		 *当前显示的面板 
		 */		
		private var curPanel		:ILeagueMatchPanel;
		/**
		 *预选赛 
		 */		
		private var preMatchPanel	:PreMatchPanel;
		/**
		 *基础联赛 
		 */		
		private var baseMatchPanel :BaseMatchPanel;
		/**
		 *冠军联赛 
		 */		
		private var championMatchPanel :ChampionMatchPanel;
		/**
		 *传奇 
		 */		
		private var legendMatchPanel	:LegendMatchPanel;
		
		public function LeagueMatchView()
		{
			super();
		}
		/**
		 *初始化 
		 * 
		 */	
		public function initUI(view:Object=null):void
		{
			_symbolSkin = Reflection.createInstance("com.football.leaguematch.view.LeagueMatchSkin");
			this.addChild(_symbolSkin);
			//缓存位图
			this.cacheAsBitmap = true;
			//面板容器
			panelContainer = new Sprite();
			_symbolSkin.addChild(panelContainer);
			panelContainer.x = 42;
			panelContainer.y = 152;
			//初始化宽高
			width = _symbolSkin.width;
			height = _symbolSkin.height;
			//初始化UI界面控件
			windowBg = _symbolSkin.windowBg;//背景框
			/*selectLineUpMc = _symbolSkin.selectLineUpMc;
			selectLineUpMc.buttonMode = true;
			selectLineUpMc.txtName.mouseEnabled = false;*/
			countryMatchMc = _symbolSkin.countryMatchMc;
			countryMatchMcOrX = countryMatchMc.x;
			//设置关闭按钮
			setCloseBtn(_symbolSkin.closeBtn);
			//设置可拖动
			dragEnabled = true;
			//添加事件
			addEvtListener();
			//初始化面板
			//显示信息
			showBaseInfo();
		}
		/**
		 *显示基本信息
		 * 
		 */		
		private function showBaseInfo():void
		{
			var country :int = AppData.getInstance().clubDatamanager.clubInfo.country;
			countryMatchMc.gotoAndStop(country);
			countryMatchMc.x = countryMatchMcOrX;
			var ntype :int = AppData.getInstance().leagueMatchManager.ntype;
			DisplayObjectUtil.removeAllChildren(panelContainer);
			_symbolSkin.topMc.visible = true;
			var tipsInfo :String ;
			switch(ntype)
			{
				case MatchType.PRE_MATCH://预选赛
				{
					if(preMatchPanel == null)
					{
						preMatchPanel = new PreMatchPanel();
						preMatchPanel.initUI();
					}
					if(preMatchPanel.parent == null)
					{
						panelContainer.addChild(preMatchPanel);
					}
					panelContainer.x = 13;
					panelContainer.y = 50;
					curPanel = preMatchPanel;
					_symbolSkin.topMc.mc1.gotoAndStop(2);
					_symbolSkin.topMc.mc1.buttonMode = true;
					//TIPs
					tipsInfo = "<p align='left'>新手联赛，全胜可晋升到甲级联赛！</p>";
					DisplayMouseEventManager.instance.addTipsDisplay(_symbolSkin.topMc.mc1,tipsInfo);
					break;
				}
				case MatchType.BASE_MATCH://基础联赛
				{
					if(baseMatchPanel == null)
					{
						baseMatchPanel = new BaseMatchPanel();
						baseMatchPanel.initUI();
					}
					if(baseMatchPanel.parent == null)
					{
						panelContainer.addChild(baseMatchPanel);
					}
					panelContainer.x = 13;
					panelContainer.y = 50;
					curPanel = baseMatchPanel;
					//countryMatchMc.preMc.visible = false;
					//countryMatchMc.x += countryMatchMc.preMc.width/2;
					_symbolSkin.topMc.mc1.gotoAndStop(1);
					_symbolSkin.topMc.mc2.gotoAndStop(2);
					_symbolSkin.topMc.mc3.gotoAndStop(1);
					_symbolSkin.topMc.mc2.buttonMode = true;
					//TIPs
					tipsInfo = "<p align='left'>赢取9场比赛可晋升到冠军联赛！</p>";
					DisplayMouseEventManager.instance.addTipsDisplay(_symbolSkin.topMc.mc2,tipsInfo);
					break;
				}
				case MatchType.CHAMPION_MATCH://冠军联赛
				{
					if(championMatchPanel == null)
					{
						championMatchPanel = new ChampionMatchPanel();
						championMatchPanel.initUI();
					}
					if(championMatchPanel.parent == null)
					{
						panelContainer.addChild(championMatchPanel);
					}
					panelContainer.x = 13;
					panelContainer.y = 50;
					curPanel = championMatchPanel;
					countryMatchMc.gotoAndStop(7);
					_symbolSkin.topMc.mc2.gotoAndStop(1);
					_symbolSkin.topMc.mc3.gotoAndStop(2);
					_symbolSkin.topMc.mc3.buttonMode = true;
					//TIPs
					tipsInfo = "<p align='left'>获得冠军可挑战传奇队伍！</p>";
					DisplayMouseEventManager.instance.addTipsDisplay(_symbolSkin.topMc.mc3,tipsInfo);
					break;
				}
				case MatchType.LEGEND_MATCH://传奇
				{
					_symbolSkin.topMc.visible = false;
					if(legendMatchPanel == null)
					{
						legendMatchPanel = new LegendMatchPanel();
						legendMatchPanel.initUI();
					}
					if(legendMatchPanel.parent == null)
					{
						panelContainer.addChild(legendMatchPanel);
					}
					panelContainer.x = 14;
					panelContainer.y = 47;
					curPanel = legendMatchPanel;
					countryMatchMc.gotoAndStop(8);
					break;
				}
			}
		}
		/**
		 *注册事件 
		 * 
		 */	
		public function addEvtListener():void
		{
			AppData.getInstance().leagueMatchManager.addEventListener(Event.CHANGE,dataChangeHandler);
		}
		/**
		 *数据改变 
		 * @param e
		 */		
		private function dataChangeHandler(e :Event):void
		{
			showBaseInfo();
		}
		/**
		 * 关闭按钮响应
		 * @param e
		 * 
		 */		
		override protected function onCloseHandler(e :MouseEvent):void
		{
			this.dispatchEvent(new ModuleEvent(ModuleEvent.REMOVE_MODULE,true));
		}
		/**
		 *删除事件 
		 * 
		 */	
		public function removeEvtListener():void
		{
			AppData.getInstance().leagueMatchManager.removeEventListener(Event.CHANGE,dataChangeHandler);
		}
		/**
		 *销毁 
		 * 
		 */	
		public function destroyView():void
		{
			if(this.parent)
			{
				this.parent.removeChild(this);
			}
			removeEvtListener();
			if(preMatchPanel)
			{
				preMatchPanel.destroyView();
				preMatchPanel = null;
			}
			if(baseMatchPanel)
			{
				baseMatchPanel.destroyView();
				baseMatchPanel = null;
			}
			if(championMatchPanel)
			{
				championMatchPanel.destroyView();
				championMatchPanel = null;
			}
			if(legendMatchPanel)
			{
				legendMatchPanel.destroyView();
				legendMatchPanel = null;
			}
			curPanel = null;
			DisplayMouseEventManager.instance.removeTipsDisplay(_symbolSkin.topMc.mc1);
			DisplayMouseEventManager.instance.removeTipsDisplay(_symbolSkin.topMc.mc2);
			DisplayMouseEventManager.instance.removeTipsDisplay(_symbolSkin.topMc.mc3);
			DisplayObjectUtil.removeAllChildren(panelContainer);
			panelContainer = null;
		}
		/**
		 *显示tab类型 
		 * @param $obj
		 */
		public function showType($obj:Object):void
		{
		}
		/********************** implements interface *********************************/
		/**
		 * 比赛开始
		 */		
		public function matchStart(value :Object = null):void
		{
			
		}
		/**
		 * 比赛结束
		 */		
		public function matchEnd(value :Object = null):void
		{
			if(curPanel)
			{
				curPanel.matchEnd();
			}
		}
		/**
		 * 错误返回
		 */		
		public function errorRes(value :Object = null):void
		{
			if(curPanel)
			{
				curPanel.errorRes();
			}
		}
	}
}